package com.bitejiuyeke.job_publish.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * 线程池配置类
 *
 * @Author 比特就业课
 */
@EnableAsync // 开启异步支持
@Configuration
public class ThreadPoolConfig {
//thread-pool
//    core-pool-size: 3 # 核心线程数
//    max-pool-size: 5  # 最大线程数
//    queue-capacity: 20 # 阻塞队列最大容量
//    keep-alive-seconds: 30 # 空闲时间(秒)
//    name-prefix: bite-async-service- # 线程名前缀

    // 核心线程数
    @Value("${thread-pool.core-pool-size}")
    private int corePoolSize;
    // 最大线程数
    @Value("${thread-pool.max-pool-size}")
    private int maxPoolSize;
    // 阻塞队列最大容量
    @Value("${thread-pool.queue-capacity}")
    private int queueCapacity;
    // 空闲时间(秒)
    @Value("${thread-pool.keep-alive-seconds}")
    private int keepAliveSeconds;
    // 线程名前缀
    @Value("${thread-pool.name-prefix}")
    private String namePrefix;

    @Bean
    public ThreadPoolTaskExecutor threadPoolTaskExecutor () {
        ThreadPoolTaskExecutor threadPool = new ThreadPoolTaskExecutor();
        threadPool.setCorePoolSize(corePoolSize);
        threadPool.setMaxPoolSize(maxPoolSize);
        threadPool.setQueueCapacity(queueCapacity);
        threadPool.setKeepAliveSeconds(keepAliveSeconds);
        threadPool.setThreadNamePrefix(namePrefix);
        // 拒绝策略
        threadPool.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        return threadPool;
    }

}
